/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PainelJogos.java
 *
 * Created on Jun 9, 2011, 6:02:30 PM
 */
package funcenter.gui;

import funcenter.main.Configuracoes;
import funcenter.main.Main;
import java.awt.GridBagConstraints;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 *
 * @author arthur
 */
public class PainelJogos extends javax.swing.JPanel {

    Configuracoes[] configJogos;
    JButton[] botoesJogos;
    JButton[] botoesJogar;
    
    /** Creates new form PainelJogos */
    public PainelJogos(Configuracoes[] configJogos) {
        initComponents();
        this.reiniciar(configJogos);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        jButton1.setText("Cata-Letra");
        jButton1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jButton1.setMinimumSize(new java.awt.Dimension(150, 40));
        jButton1.setPreferredSize(new java.awt.Dimension(150, 40));
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                clicarBotao(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clicarBotaoDescricao(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 5, 5);
        add(jButton1, gridBagConstraints);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/funcenter/images/play.png"))); // NOI18N
        jButton3.setMargin(new java.awt.Insets(2, 6, 2, 6));
        jButton3.setPreferredSize(new java.awt.Dimension(46, 40));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 5, 5);
        add(jButton3, gridBagConstraints);

        jButton5.setText("Minado");
        jButton5.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jButton5.setMinimumSize(new java.awt.Dimension(150, 40));
        jButton5.setPreferredSize(new java.awt.Dimension(150, 40));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jButton5, gridBagConstraints);

        jButton6.setText("Sudoku");
        jButton6.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jButton6.setMinimumSize(new java.awt.Dimension(150, 40));
        jButton6.setPreferredSize(new java.awt.Dimension(150, 40));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jButton6, gridBagConstraints);

        jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/funcenter/images/play.png"))); // NOI18N
        jButton7.setMargin(new java.awt.Insets(2, 6, 2, 6));
        jButton7.setPreferredSize(new java.awt.Dimension(46, 40));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jButton7, gridBagConstraints);

        jButton8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/funcenter/images/play.png"))); // NOI18N
        jButton8.setMargin(new java.awt.Insets(2, 6, 2, 6));
        jButton8.setPreferredSize(new java.awt.Dimension(46, 40));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jButton8, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void clicarBotaoDescricao(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clicarBotaoDescricao
        
    }//GEN-LAST:event_clicarBotaoDescricao

    private void clicarBotao(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_clicarBotao
        for(int i = 0; i < botoesJogos.length; i++){
            if(evt.getComponent().equals(botoesJogos[i])){
                Main.getJanelaPrincipal().trocarPainelDescricao(i);
            }
        }
    }//GEN-LAST:event_clicarBotao

    private void clicarJogar(java.awt.event.MouseEvent evt) {
        for(int i = 0; i < botoesJogar.length; i++){
            if(evt.getComponent().equals(botoesJogar[i])){
                String so = System.getProperty("os.name").toLowerCase();
                System.out.println(so + " " + configJogos[i].getComandoLin());
                try {
                    if(!configJogos[i].getComandoLin().equals("")){
                        if(so.contains("linux")){
                            Runtime.getRuntime().exec(configJogos[i].getComandoLin());
                        } else if(so.contains("windows")){
                            Runtime.getRuntime().exec(configJogos[i].getComandoWin());
                        } else if(so.contains("mac")){
                            Runtime.getRuntime().exec(configJogos[i].getComandoMac());
                        }
                    } else {
                        JOptionPane.showMessageDialog(Main.getJanelaPrincipal(), "Este jogo não é compativel com o seu sistema operacional.");
                    }
                } catch(IOException ex){
                    Logger.getLogger(PainelJogos.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    // End of variables declaration//GEN-END:variables


    public final void reiniciar(Configuracoes[] configJogos){
        this.removeAll();
        
        this.configJogos = configJogos;
        botoesJogos = new JButton[configJogos.length];
        botoesJogar = new JButton[configJogos.length];
        
        GridBagConstraints gbConf = new GridBagConstraints();
        
        for(int i = 0; i < configJogos.length; i++){
            botoesJogos[i] = new JButton(configJogos[i].getNomeJogo());
            botoesJogos[i].setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
            botoesJogos[i].setMinimumSize(new java.awt.Dimension(150, 40));
            botoesJogos[i].setPreferredSize(new java.awt.Dimension(150, 40));
            botoesJogos[i].addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    clicarBotao(evt);
                }
            });
            gbConf.gridx = 0;
            gbConf.gridy = i;
            gbConf.insets = new java.awt.Insets(5, 5, 5, 0);
            add(botoesJogos[i], gbConf);
            
            botoesJogar[i] = new JButton(new ImageIcon(getClass().getResource("/funcenter/images/play.png")));
            botoesJogar[i].setMargin(new java.awt.Insets(2, 6, 2, 6));
            botoesJogar[i].setPreferredSize(new java.awt.Dimension(46, 40));
            botoesJogar[i].addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    clicarJogar(evt);
                }
            });
            gbConf.gridx = 1;
            gbConf.insets = new java.awt.Insets(5, 0, 5, 5);
            add(botoesJogar[i], gbConf);
        }
    }
}
